package com.ext.aic.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.ext.aic.AICApplication;
import com.ext.aic.beans.box.ChatViewElement;
import com.ext.aic.beans.box.ConversationViewElement;
import com.ext.aic.beans.box.WxElement;
import com.eyinfo.android_pure_utils.utils.JsonUtils;
import com.eyinfo.android_pure_utils.utils.ObjectJudge;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class WxElementUtils {

    private static volatile WxElementUtils instance;

    public static WxElementUtils getInstance() {
        if (instance == null) {
            synchronized (WxElementUtils.class) {
                if (instance == null) {
                    instance = new WxElementUtils();
                }
            }
        }
        return instance;
    }

    //region 属性
    private WxElement wxElement;
    private ConversationViewElement conversationWindowElements;
    private ChatViewElement chatWindowElements;

    private List<String> wxLoginMarks;
    //endregion

    private void detector() {
        if (wxElement == null) {
            loadIdElements();
        }
        if (wxLoginMarks == null) {
            loadWxLoginMarksElements();
        }
    }

    private void loadIdElements() {
        try {
            Context applicationContext = AICApplication.getInstance().getApplicationContext();
            AssetManager assetManager = applicationContext.getAssets();
            InputStream inputStream = assetManager.open("wx_elements.json", AssetManager.ACCESS_STREAMING);
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            int read = inputStream.read(buffer);
            inputStream.close();
            String content = new String(buffer, StandardCharsets.UTF_8);
            wxElement = JsonUtils.parseT(content, WxElement.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void loadWxLoginMarksElements() {
        try {
            Context applicationContext = AICApplication.getInstance().getApplicationContext();
            AssetManager assetManager = applicationContext.getAssets();
            InputStream inputStream = assetManager.open("login_mark.json", AssetManager.ACCESS_STREAMING);
            int size = inputStream.available();
            byte[] buffer = new byte[size];
            int read = inputStream.read(buffer);
            inputStream.close();
            String content = new String(buffer, StandardCharsets.UTF_8);
            wxLoginMarks = JsonUtils.parseArray(content, String.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //region 获取会话相关元素

    public ConversationViewElement getConversationWindowElements() {
        if (conversationWindowElements == null || ObjectJudge.isNullOrEmpty(conversationWindowElements.getListTopViewIds())) {
            detector();
        }
        conversationWindowElements = wxElement.getConversationWindow();
        return conversationWindowElements;
    }

    public ChatViewElement getChatWindowElements() {
        if (chatWindowElements == null || ObjectJudge.isNullOrEmpty(chatWindowElements.getMarkViewIds())) {
            detector();
        }
        chatWindowElements = wxElement.getChatWindow();
        return chatWindowElements;
    }
    //endregion

    //region 获取微信登录标记
    public List<String> getWxLoginMarks() {
        if (ObjectJudge.isNullOrEmpty(wxLoginMarks)) {
            detector();
        }
        return wxLoginMarks;
    }
    //endregion
}
